var jfJQMDialog = (function($, document, window) {

  /*************** public *************************/
  
  var that = {
    showDialogOnce: function(url, changePageOptions) {
      _showDialogOnce(localStorage, url, changePageOptions);
    },
    showDialogOncePerSession: function(url, changePageOptions) {
      _showDialogOnce(sessionStorage, url, changePageOptions);
    },
    showDialogOnceIfMinimumUsageCount: function(minimumUsageCount, url, changePageOptions) {
      return _showDialogOnceIfMinimumUsageCount(localStorage, url, changePageOptions);
    },
    showDialogOncePerSessionIfMinimumUsageCount: function(minimumUsageCount, url, changePageOptions) {
      return _showDialogOnceIfMinimumUsageCount(sessionStorage, url, changePageOptions);
    },
    showDialogIfMinimumUsageCount: function(minimumUsageCount, url, changePageOptions) {
      if (getUsageCount() >= minimumUsageCount) {
        that.showDialog(url, changePageOptions);
        return true;
      }
      
      return false;
    },
    showDialog: function(url, changePageOptions) {
      changePageOptions = $.extend(true, {role: 'dialog', 'showLoadMsg': false}, changePageOptions);
      $.mobile.changePage(url, changePageOptions);
    }
  }
  
  /*************** private **********************/
  
  /*
  document.addEventListener("resume", onResume, false);
  function onResume() {
    incrementUsageCount();
  }
  */
  
  var usageCountKey = 'jfJQMDialog_usageCount';

  var localStorage = window.localStorage ? window.localStorage : { setItem: function() {}, getItem: function() { return null; } };
  var sessionStorage = window.sessionStorage ? window.sessionStorage : { setItem: function() {}, getItem: function() { return null; } };
  
  function incrementUsageCount() {
    setUsageCount(getUsageCount()+1);
  }
  function getUsageCount() {
    return parseInt(localStorage.getItem(usageCountKey), 10) || 0;
  }
  function setUsageCount(value) {
    return localStorage.setItem(usageCountKey, value);
  }
  
  function markUrlAsVisited(url, storage) {
    if (!urlWasVisited(url, storage)) {
      var visitedUrls = getVisitedUrls(storage);
      visitedUrls.push(url);
      setVisitedUrls(visitedUrls, storage);
    }
  }
  function urlWasVisited(url, storage) {
    var visitedUrls = getVisitedUrls(storage);
    return -1 !== visitedUrls.indexOf(url);
  }
  function getVisitedUrls(storage) {
    var storage = storage || localStorage;
    return JSON.parse(storage.getItem('jfJQMDialog_visitedUrls') || '[]');
  }
  function setVisitedUrls(value, storage) {
    var storage = storage || localStorage;
    storage.setItem('jfJQMDialog_visitedUrls', JSON.stringify(value));
  }
  
  function _showDialogOnce(storage, url, changePageOptions) {
    if (!urlWasVisited(url, storage)) {
      that.showDialog(url, changePageOptions);
      markUrlAsVisited(url, storage);
      return true;
    }

    return false;
  }
  
  function _showDialogOnceIfMinimumUsageCount(storage, minimumUsageCount, url, changePageOptions) {
    if (!urlWasVisited(url, storage)) {
      if (that.showDialogIfMinimumUsageCount(minimumUsageCount, url, changePageOptions)) {
        markUrlAsVisited(url, storage);
        return true;
      }
    }

    return false;
  }
  
  /*************** init *************************/
  
  incrementUsageCount();
  
  return that;
  
})(jQuery, document, window);